package com.sjk.flink.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author SAMA
 */
public class MessyCodeCheck {
    private static Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");

    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.LETTERLIKE_SYMBOLS
                || ub == Character.UnicodeBlock.BASIC_LATIN
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY
                || ub == Character.UnicodeBlock.LATIN_1_SUPPLEMENT
        ) {
            return true;
        }
        return false;
    }


    public static boolean isMessyCode(String strName) {
        Matcher m = p.matcher(strName);
        String after = m.replaceAll("");
        //TODO 从定义文件中获得
        //String specialSymbols = after.replaceAll("[℃$㎡m³E+°?=]", "");
        String temp = after.replaceAll("\\p{P}", "");
        char[] ch = temp.trim().toCharArray();
        float chLength = ch.length;
        float count = 0;
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (!Character.isLetterOrDigit(c)) {
                if (!isChinese(c)) {
                    count = count + 1;
                    //System.out.print(c);
                }
            }
        }
        //float result = count / chLength;
        //System.out.println("#[ " + count);
        //System.out.println("# " + result);
        if (count > 0) {
            return true;
        } else {
            return false;
        }

    }

    public static void main(String[] args) {
        //System.out.println(Character.UnicodeBlock.of('℃'));
        //System.out.println(Character.UnicodeBlock.of('$'));
        System.out.println(Character.UnicodeBlock.of('㎡'));
        System.out.println(Character.UnicodeBlock.of('³'));
        //System.out.println(Character.UnicodeBlock.of('+'));
        //System.out.println(Character.UnicodeBlock.of('°'));
        //System.out.println(Character.UnicodeBlock.of('?'));
        //System.out.println(Character.UnicodeBlock.of('='));
        //System.out.println(isMessyCode("*��JTP.jar�ļ����JTP�ļ���ȡ��ͼƬ��Դ"));
        String msg = "{\"sscsdm\":\"320583\",\"extract_time\":\"2023-11-22 07:52:23.577\",\"record\":\"[{\\\"JCYJSJ\\\":\\\"2023-11-22 15:50:37\\\",\\\"JCYJXQ\\\":\\\"柏庐路柏庐广场球工地车辆视频检测事件\\\",\\\"XZQHDM\\\":\\\"320100\\\",\\\"XSD\\\":\\\"88.88%\\\",\\\"JCSSID\\\":\\\"32058351001310019080\\\",\\\"YJWJ\\\":\\\"http://172.18.179.3:2222/home/ftpkunshan/KS/SG/SG_JC_SPJCSBJCYJ/YJWJ/20231122/c578ea800f77451c9a95500e650b9117.jpg\\\",\\\"YJMC\\\":\\\"工地车辆视频检测事件\\\",\\\"CZZT\\\":\\\"2\\\",\\\"JCYJLX\\\":\\\"3\\\",\\\"imageURL\\\":\\\"http://2.36.31.57:6120/pic?0d9c00=4080ip-6eo701-b67*fo2=5826*3l1=3403693790t0=518**ps==911b*=c44f*7c317ca57-ba400e-0*l1b8od06272=003\\\",\\\"BZ\\\":\\\"工地车辆报警\\\",\\\"KJXX\\\":\\\"\\\",\\\"BSM\\\":\\\"1075511755e74c76bdaefc81e1e64cb4\\\",\\\"YJBH\\\":\\\"c578ea800f77451c9a95500e650b9117\\\"}]\"}";
        System.out.println(isMessyCode(msg));
        System.out.println(isMessyCode("http://2.36.31.57:6120/pic?0d9c00=4080ip-6eo701-b67*fo2=5826*3l1=3403693790t0=518**ps==911b*=c44f*7c317ca57-ba400e-0*l1b8od06272=003"));
        System.out.println(isMessyCode("你好m³"));
        /***

        System.out.println(isMessyCode("监测值单位:℃"));
        System.out.println(isMessyCode("监测值单位:$"));
        System.out.println(isMessyCode("沥青路面10㎡。据多方了解"));

        String json = "{\n" +
                "\"jsonArr\": [\n" +
                "   {\n" +
                "       \"firstName\":\"�ļ����JTP�ļ\",\n" +
                "       \"lastName\":\"Smith\"\n" +
                "   },\n" +
                "   {\n" +
                "       \"firstName\":\"Bob\",\n" +
                "       \"lastName\":\"�ļ����JTP�ļ\"\n" +
                "   },\n" +
                "   {\n" +
                "       \"firstName\":\"Carol\",\n" +
                "       \"lastName\":\"Johnson\"\n" +
                "   }\n" +
                "   ]\n" +
                "}";
         **/
        //System.out.println(isMessyCode("{\"sscsdm\":\"320100\",\"extract_time\":\"2023-11-15 03:35:33.178\",\"record\":\"{\\\"bsm\\\":\\\"3d868e6c-3e46-4c04-9d7f-2dcb76bc4fa8\\\",\\\"jcssid\\\":\\\"8.69E+14\\\",\\\"kjxx\\\":\\\"(118.772232,32.018777)\\\",\\\"yxsj\\\":\\\"2023-7-13 9:22\\\",\\\"yxzt\\\":1,\\\"yskzjbz\\\":\\\"3d868e6c-3e46-4c04-9d7f-2dcb76bc4fa8\\\",\\\"sjtbzt\\\":\\\"I\\\",\\\"tbsj\\\":\\\"2023/11/15 11:36:05\\\"}\"}"));
        //System.out.println(">>>>>>>>>>>>     "+isMessyCode(json));
    }
}
